package com.jmb.api.web.controller.interfaces;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmb.api.common.result.Result;
import com.jmb.api.common.utils.LoginUserInfo;
import com.jmb.api.common.utils.NoUtil;
import com.jmb.api.entity.ExecuteInterfaceCaseParam;
import com.jmb.api.entity.InterfaceCase;
import com.jmb.api.vo.Interface.Case.Execute.InterfaceCaseExecuteLogVO;
import com.jmb.api.vo.Interface.Case.InterfaceCaseInfoVO;
import com.jmb.api.vo.Interface.Case.InterfaceCaseListTO;
import com.jmb.api.vo.Interface.Case.InterfaceCaseQueryVO;
import com.jmb.api.vo.Interface.Case.InterfaceCaseTO;
import com.jmb.api.web.service.InterfaceCaseExecuteLogService;
import com.jmb.api.web.service.InterfaceCaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Slf4j
@Tag(name = "接口用例管理")
@RestController
@RequestMapping("/web/interfaceCase")
public class InterfaceCaseController {
    @Autowired
    private InterfaceCaseService interfaceCaseService;
    @Autowired
    private LoginUserInfo loginUserInfo;
    @Autowired
    private InterfaceCaseExecuteLogService interfaceCaseExecuteLogService;


    // 新增接口用例
    @Operation(summary = "新增接口用例")
    @PostMapping("/saveOrUpdateInterfaceCase")
    public Result saveInterfaceCase(@RequestBody @Valid InterfaceCaseTO interfaceCase){
        interfaceCaseService.saveInterfaceCaseAndAssertAndPostProcessorAndPreCase(interfaceCase);
        return Result.ok();
    }
    // 查询接口用例列表
    @Operation(summary = "查询接口用例列表")
    @GetMapping("/listInterfaceCase")
    public Result listInterfaceCase(InterfaceCaseQueryVO interfaceCase){
        IPage<InterfaceCaseQueryVO> page = new Page<>(interfaceCase.getPageNum(),interfaceCase.getPageSize());
        IPage<InterfaceCaseQueryVO> list = interfaceCaseService.listInterfaceCase(page, interfaceCase);

        return Result.ok(list);
    }
    // 执行接口测试用例
    @Operation(summary = "执行接口测试用例")
    @GetMapping("/executeInterfaceCase/{caseId}")
    public Result executeInterfaceCase(@PathVariable Long caseId){
        // 获取用户名
        String username = loginUserInfo.Username();
        try {
            ExecuteInterfaceCaseParam caseParam = new ExecuteInterfaceCaseParam(caseId, username,
                    null, NoUtil.genChainNo(), null, 1,
                    null, null, null, null,
                    0, null, false);
            Long executorLog = interfaceCaseService.executeInterfaceCase(caseParam);
            log.info("------------------------------用例执行完毕，用例编号= {} 执行日志编号= {}",caseId, executorLog);
            Integer status = interfaceCaseExecuteLogService.findExecute(executorLog).getStatus();
            if (status == 0){
                log.info("-------------------------------------------用例执行成功");
                return Result.ok("执行成功");
            }else if (status == 1){
                log.info("-------------------------------------------用例执行失败");
                return Result.fail("执行失败");
            }else {
                log.info("-------------------------------------------用例执行异常");
                return Result.fail("执行异常");
            }
        }catch (Exception e){
            log.error("执行用例异常：",e.getMessage());
            return Result.fail("执行异常");
        }
    }
}
